package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ysd.entity.Product;
import com.ysd.util.DBManager;

public class ChanPinSoSuoDaoImpl {
	 
	public ArrayList<Product> getChanPinSuoSou(String name) {
		ArrayList<Product> soSouList = new ArrayList<Product>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {// 正常
				// 2.创建Statement对象
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * from product where productName like '%"+name+"%'";
			// 4.执行sql语句
			rs = st.executeQuery(sql);// execute执行Query查询
			// 5.遍历结果集
			while (rs.next()) {
				int productId = rs.getInt("productId");
				String productName = rs.getString("productName");
				String productImg = rs.getString("productImg");
				Product product = new Product(productId, productName, productImg);
				soSouList.add(product);
			}
			return soSouList;// 正常执行返回用户集合
		} catch (SQLException e) {// 异常
			e.printStackTrace();
			return null;// 异常执行返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
}
